﻿#include "TestgRpcClientPluginImp.h"
#include "TestgRpcClient.h"


extern "C" OSGI_EXPORT_SYMBOL void * K5_Bundle_Activator(void)
{
    IBundleActivator* pBund = 0;
    pBund = TestgRpcClientPluginImp::Instance();
    return (void*)pBund;
}

TestgRpcClientPluginImp* TestgRpcClientPluginImp::m_pInstance = NULL;
TestgRpcClientPluginImp* TestgRpcClientPluginImp::Instance()
{
    if(m_pInstance == NULL) m_pInstance = new TestgRpcClientPluginImp();
    return m_pInstance;
}

TestgRpcClientPluginImp::TestgRpcClientPluginImp(void)
{
    m_pBundCnt = 0;
    m_pServMgr = 0;
    m_pAlloc = 0;
    //m_pClusterFactory = 0;
    m_pIniParser = 0;
    m_pLogger = 0;
}

TestgRpcClientPluginImp::~TestgRpcClientPluginImp(void)
{
    delete m_pBundCnt;
    m_pBundCnt = 0;
    delete m_pServMgr;
    m_pServMgr = 0;
    delete m_pAlloc;
    m_pAlloc =0;
    //delete m_pClusterFactory;
    //m_pClusterFactory = 0;
    delete m_pIniParser;
    m_pIniParser = 0;
    delete m_pLogger;
    m_pLogger = 0;
}

bool TestgRpcClientPluginImp::setServiceMnager(IBundleContext* pBundleContext)
{
    IServiceManager * pServiceManager = 0;
    int iRet = pBundleContext->GetServiceManager(&pServiceManager);
    if((SUCCESS != iRet) || (0 == pServiceManager))
    {
        m_pServMgr = 0;
        return false;
    }
    m_pServMgr = pServiceManager;
    return true;
}

bool TestgRpcClientPluginImp::setBundleContext(IBundleContext* pBundleContext)
{
    if(0 == pBundleContext)
        return false;
    m_pBundCnt = pBundleContext;
    return true;
}

bool TestgRpcClientPluginImp::setAllocator(IBundleContext* pBundleContext)
{
    IAllocator * pAllocator = 0;
    pBundleContext->GetAllocator(&pAllocator);
    if(0 == pAllocator) return false;
    m_pAlloc = pAllocator;
    return true;
}

#if 1
//预留接口，后期如果插件需要配置，可以使用这个接口读取配置
bool TestgRpcClientPluginImp::setIniConfigParser(IBundleContext* pBundleContext)
{
    IService * pServiceFacConfig = 0;
    m_pServMgr->GetService(IID_ConfigParserFactory, &pServiceFacConfig, SID_ConfigParserFactory, 0);
    if(0 == pServiceFacConfig)
        return false;
    IConfigParserFactory * pConfigParserFactory = (IConfigParserFactory*)pServiceFacConfig;
    m_pIniParser = (IINIConfigParser*)pConfigParserFactory->CreateConfigParser("TestGRPCClient.ini","TestgRpcClientPlugin",INI);
    if(0 == m_pIniParser)
        return false;

    const char * pValue = 0;

    //获取gRpc服务端配置ip
    if(false == m_pIniParser->GetValueOrDefault("Client","serverIP",&pValue,"0.0.0.0")) return false;
    server_address = pValue;
    printf("获得gRpc服务端配置ini文件中%s节点下%s参数的信息为: %s .\n","Server","serverIP",server_address.c_str());

    //获取gRpc服务端配置port
    if(false == m_pIniParser->GetValueOrDefault("Client","serverPort",&pValue,"50051")) return false;
    server_address = server_address + ":" + pValue;
    int ServerPort = atoi(pValue);
    printf("获得gRpc服务端配置ini文件中%s节点下%s参数的信息为: %d .\n","Server","serverPort",ServerPort);
    printf("gRpc服务端配置IP端口信息为: %s .\n",server_address.c_str());
    printf("获取gRpc服务端配置ini信息完成\n");

    return true;
}
#endif

bool TestgRpcClientPluginImp::setLogger(IBundleContext* pBundleContext)
{
    IService * pLogFac = 0;
    m_pServMgr->GetService(IID_LoggerFactory,&pLogFac,SID_LoggerFactory,0);
    if(0 == pLogFac)
        return false;
    ILoggerFactory *pLoggerFactory = (ILoggerFactory *)pLogFac;

    m_pLogger = pLoggerFactory->CreateBundleLogger("TestGRPCClient.log","TestgRpcClientPlugin");
    if(m_pLogger == NULL) return false;
    return true;
}

/*
bool TestgRpcClientPluginImp::setClusterManagerFac(IBundleContext* pBundleContext)
{
    IService * pServiceFacCluster = 0;
    m_pServMgr->GetService(IID_ClusterFactory, &pServiceFacCluster, SID_ClusterFactory, 0);
    if(0 == pServiceFacCluster)
        return false;
    m_pClusterFactory = (IClusterFactory*)pServiceFacCluster;
    return true;
}
*/

ILogger * TestgRpcClientPluginImp::GetLogger()
{
    return m_pLogger;
}

bool MKRAPI TestgRpcClientPluginImp::Start(IBundleContext * pBundleContext)
{

    bool bRet = false;
    try
    {
        //初始化
        if(setBundleContext(pBundleContext) != true)
            return bRet;
        if(setAllocator(pBundleContext) != true)
            return bRet;
        if(setServiceMnager(pBundleContext) != true)
            return bRet;
        if(setIniConfigParser(pBundleContext) != true)
            return bRet;
        if(setLogger(pBundleContext) != true)
            return bRet;

        //readConfig(server_address);

        m_pLogger->DebugLog("11111", "开始TestGRPCClient测试");
        TestGRPCClient * pTestGRPCClient = TestGRPCClient::Instance();
#if 1
        //同步测试用例的调用,同步例子的异步例子只启用一种，暂时目前测试例子中同步异步配置文件是使用的同一个
        pTestGRPCClient->TestgRPCSynClientRun(server_address);
#else

#if 0
        //异步测试用例的调用，需要调用者自己处理回调信息，比如下边的例子
        pTestGRPCClient->TestgRPCAsynClientRun(server_address);
#else
        //异步测试方法二用例的调用，需要调用者自己处理回调信息，比如下边的例子
        pTestGRPCClient->TestgRPCAsynClient2Run(server_address);
#endif

#endif
        m_pLogger->DebugLog("22222", "开始TestGRPCClient测试");
        m_pLogger->DebugLog("33333", "开始注册服务IID_GRPC_TestClient和SID_GRPC_TestClient");
        IService * pTestgRpcClient = 0;
        //TestGRPCClient * pTestgRpcService = TestGRPCServer::Instance();
        pTestgRpcClient = (TestGRPCClient *)pTestGRPCClient;
        m_pServMgr->RegisterService(IID_GRPC_TestClient,pTestgRpcClient,SID_GRPC_TestClient,NULL);

        bRet = true;

    }
    catch(...)
    {
        bRet = false;
    }

    return bRet;
}

bool MKRAPI TestgRpcClientPluginImp::Stop(IBundleContext * pBundleContext)
{
    return true;
}

bool TestgRpcClientPluginImp::readConfig(std::string server_address)
{

    IService * pServiceFacConfig = 0;
    m_pServMgr->GetService(IID_ConfigParserFactory, &pServiceFacConfig, SID_ConfigParserFactory, 0);
    if(0 == pServiceFacConfig)
        return false;

    IConfigParserFactory * pConfigParserFactory = (IConfigParserFactory*)pServiceFacConfig;

    //从配置文件中获取基础配置
    IINIConfigParser * pIniConfigParser = (IINIConfigParser *)pConfigParserFactory->CreateConfigParser("TestgRpcPlugin.ini","TestgRpcServicePlugin",INI);
    if(0 == pIniConfigParser) return false;

    const char * pValue = 0;

    //获取gRpc服务端配置ip
    if(false == pIniConfigParser->GetValueOrDefault("Client","serverIP",&pValue,"0.0.0.0")) return false;
    server_address = pValue;
    printf("获得gRpc服务端配置ini文件中%s节点下%s参数的信息为: %s .\n","Server","serverIP",server_address.c_str());

    //获取gRpc服务端配置port
    if(false == pIniConfigParser->GetValueOrDefault("Client","serverPort",&pValue,"50051")) return false;
    server_address = server_address + ":" + pValue;
    int ServerPort = atoi(pValue);
    printf("获得gRpc服务端配置ini文件中%s节点下%s参数的信息为: %d .\n","Server","serverPort",ServerPort);
    printf("gRpc服务端配置IP端口信息为: %s .\n",server_address.c_str());
    printf("获取gRpc服务端配置ini信息完成\n");
    return true;
}
#if 0
void TestgRpcClientPluginImp::registerToCluster(std::string &strServiceName,ServerServiceName &info)
{
    ICluster * serviceCluster = m_pClusterFactory->CreateCluster(strServiceName.c_str());
    SERVICE_BASEINFO baseInfo;
    baseInfo.pAddr = info.ip;
    baseInfo.port = info.port;
    baseInfo.protocol = 0;//wei ding yi
    IDictionary * instanceInfo = (IDictionary *)CreateDictionary();;
    char * instanceID = 0;

    serviceCluster->RegServiceInstance(&baseInfo,instanceInfo,&instanceID);
}
#endif


